/*
 * @Author: zhy 1853663824@qq.com
 * @Date: 2025-03-31 15:49:40
 * @LastEditors: zhy 1853663824@qq.com
 * @LastEditTime: 2025-04-01 10:20:14
 * @FilePath: \java-air-plant-master\air-dictionary\src\main\java\zhongbo\java\air\dictionary\service\impl\AirPortServiceImpl.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package zhongbo.java.air.dictionary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import zhongbo.java.air.dictionary.mapper.AirportMapper;
import zhongbo.java.air.dictionary.model.Airport;
import zhongbo.java.air.dictionary.service.IAirportService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AirportServiceImpl extends ServiceImpl<AirportMapper, Airport> implements IAirportService {

    @Override
    public Page<Airport> pageWithOrder(Page<Airport> page, String orderField, boolean isAsc) {
        QueryWrapper<Airport> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public IPage<Airport> pageWithOrder(IPage<Airport> page, String orderField, boolean isAsc) {
        QueryWrapper<Airport> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public Airport getByIcaoCode(String icaoCode) {
        LambdaQueryWrapper<Airport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Airport::getIcaoCode, icaoCode);
        return getOne(queryWrapper);
    }

    @Override
    public Airport getByIataCode(String iataCode) {
        LambdaQueryWrapper<Airport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Airport::getIataCode, iataCode);
        return getOne(queryWrapper);
    }

    @Override
    public List<Airport> listByStatus(String status) {
        LambdaQueryWrapper<Airport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Airport::getStatus, status);
        return list(queryWrapper);
    }
    
    @Override
    public boolean save(Airport entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }
    
    @Override
    public boolean updateById(Airport entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }
}
